﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameStart : MonoBehaviour
{

    private GameObject attackObj;

    public AudioSource m_Audio;

    private AudioClip m_audioClip;

    // Start is called before the first frame update


    private void Awake()
    {
        GameObject.DontDestroyOnLoad(this.gameObject);
        ABManager.Instance.LoadAssetBundleConfig();
        ResourceManager.Instance.InitAsync(this);
        ObjectManager.Instance.Init(GameObject.Find("GameStart/RecyclePoolTs").transform, GameObject.Find("GameStart/SceneTs").transform); ;
    }

    void Start()
    {

        ObjectManager.Instance.PreLoadGameobject("Assets/Prefabs/Attack.prefab");

      //  ObjectManager.Instance.InstantiateObjectAsync("Assets/Prefabs/Attack.prefab", OnInstanceResLoadFinish, LoadResPriority.Res_Heigh,true);

       // attackObj= ObjectManager.Instance.InstantiateObject("Assets/Prefabs/Attack.prefab",true);

       // ResourceManager.Instance.PreLoadRes("Assets/GameData/Sounds/senlin.mp3");
        //同步加载
        //  audioClip =  ResourceManager.Instance.LoadResource<AudioClip>("Assets/GameData/Sounds/senlin.mp3");
        //m_Audio.clip = audioClip;
        //m_Audio.Play();
        //异步加载
        //ResourceManager.Instance.InitAsync(this);
        //ResourceManager.Instance.AsnycLoadResource("Assets/GameData/Sounds/senlin.mp3", OnAudioLoadFinish, LoadResPriority.Res_Heigh);
    }


    /// <summary>
    /// 当实例化资源加载完毕
    /// </summary>
    /// <param name="path"></param>
    /// <param name="obj"></param>
    /// <param name="param1"></param>
    /// <param name="param2"></param>
    /// <param name="param3"></param>
    void OnInstanceResLoadFinish(string path,Object obj,object param1=null, object param2 = null, object param3 = null)
    {
        if (obj!=null)
        {
            attackObj = obj as GameObject;
        }
    }

    void OnAudioLoadFinish(string path,Object obj,object parm1, object parm2, object parm3)
    {
        m_audioClip= obj as AudioClip;
        m_Audio.clip = m_audioClip;
        m_Audio.Play();
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {

            ObjectManager.Instance.ReleaseObject(attackObj);
            attackObj = null;

            //m_audioClip = ResourceManager.Instance.LoadResource<AudioClip>("Assets/GameData/Sounds/senlin.mp3");
            //m_Audio.clip = m_audioClip;
            //m_Audio.Play();

            //this.m_Audio.Stop();
            //ResourceManager.Instance.ReleaseResource(m_audioClip,true);
            //m_Audio.clip = null;
            //m_audioClip = null;
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            attackObj= ObjectManager.Instance.InstantiateObject("Assets/Prefabs/Attack.prefab", true);
            //ResourceManager.Instance.ReleaseResource(m_audioClip,true);
            //m_Audio.clip = null;
            //m_audioClip = null;
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            ObjectManager.Instance.ReleaseObject(attackObj,0,true);
            attackObj = null;
        }
    }

   private void OnApplicationQuit()
    {
#if UNITY_EDITOR
        ResourceManager.Instance.ClearCache();
        Resources.UnloadUnusedAssets();
#endif
    }
}
